package com.fjrz.bbxwj.main.host.adpter

import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.fjrz.bbxwj.entity.VideoCateList
import com.fjrz.bbxwj.main.video.base.VideoHomeFragment

class ViewPager2Adapter(fragment: Fragment) : FragmentStateAdapter(fragment) {

    private val mVideoCateLists: MutableList<VideoCateList> = mutableListOf()

    private val mVideoCateHashCodes: MutableList<Long> = mutableListOf()

    override fun getItemCount(): Int {
        return mVideoCateLists.size
    }

    override fun createFragment(position: Int): Fragment {
        return VideoHomeFragment.newInstance(mVideoCateLists[position].id)
    }

    override fun getItemId(position: Int): Long {
        return mVideoCateLists[position].hashCode().toLong()
    }

    override fun containsItem(itemId: Long): Boolean {
        return mVideoCateHashCodes.contains(itemId)
    }


    fun add(videoCateList: VideoCateList) {
        mVideoCateLists.add(videoCateList)
        notifyDataSetChanged()
    }

    fun add(index: Int, videoCateList: VideoCateList) {
        mVideoCateLists.add(index, videoCateList)
        notifyDataSetChanged()
    }

    fun addALL(videoCateLists: MutableList<VideoCateList>) {
        mVideoCateLists.addAll(videoCateLists)
        notifyDataSetChanged()
    }

    fun remove(index: Int) {
        mVideoCateLists.removeAt(index)
        notifyItemRemoved(index)
    }

    fun remove(videoCateList: VideoCateList) {
        mVideoCateLists.remove(videoCateList)
        notifyDataSetChanged()
    }

    fun getVideoCateLists(): MutableList<VideoCateList> = mVideoCateLists

}